package com.qf.petmall.starter.hometopic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.petmall.starter.hometopic.entity.Hometopic;
import com.qf.petmall.starter.hometopic.mapper.HometopicMapper;
import com.qf.petmall.starter.hometopic.service.IHometopicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.petmall.starter.top_img.entity.Top_img;
import com.qf.petmall.starter.top_img.service.ITopImgService;
import com.qf.petmall.starter.topic_img.entity.Topic_img;
import com.qf.petmall.starter.topic_img.service.ITopicImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商城页面的商品列表点进去的商品详情页面模块 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-12-06
 */
@Service
public class HometopicServiceImpl extends ServiceImpl<HometopicMapper, Hometopic> implements IHometopicService {

    @Autowired
    ITopicImgService iTopicImgService;

    @Autowired
    ITopImgService iTopImgService;

   /* @Transactional(rollbackFor = Exception.class)
    @Override
    public List<Hometopic> listall() {

        List<Hometopic> list = this.list();


        List<Topic_img> list1 = iTopicImgService.list();

        List<Top_img> list2 = iTopImgService.list();

        for (Hometopic hometopic : list) {
            ArrayList<Top_img> top_imgs = new ArrayList<>();

            ArrayList<Topic_img> topic_imgs = new ArrayList<>();
            for (Topic_img topic_img : list1) {
                if(hometopic.getId()==topic_img.getParentId()){
                    topic_imgs.add(topic_img);
                }
            }
            for (Top_img top_img : list2) {
                if(hometopic.getId()==top_img.getParentId()){
                    top_imgs.add(top_img);
                }
            }
            hometopic.setTop_img(top_imgs);
            hometopic.setTopic_img(topic_imgs);
        }

        return list;
    }*/

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveone(Hometopic hometopic) {
        this.save(hometopic);
        List<Top_img> top_img = hometopic.getTop_img();
        for (Top_img topImg : top_img) {
            topImg.setParentId(hometopic.getId());
        }
        iTopImgService.saveBatch(top_img);
        List<Topic_img> topic_img = hometopic.getTopic_img();
        for (Topic_img topicImg : topic_img) {
            topicImg.setParentId(hometopic.getId());
        }
        iTopicImgService.saveBatch(topic_img);
    }

    @Override
    public boolean move(Integer id) {
        this.removeById(id);
        QueryWrapper<Top_img> top_imgQueryWrapper = new QueryWrapper<>();
        top_imgQueryWrapper.eq("parent_id",id);
        iTopImgService.remove(top_imgQueryWrapper);

        LambdaQueryWrapper<Topic_img> topic_imgLambdaQueryWrapper = new LambdaQueryWrapper<>();

        topic_imgLambdaQueryWrapper.eq(Topic_img::getParentId,id);

        iTopicImgService.remove(topic_imgLambdaQueryWrapper);
        return true;
    }

    @Override
    public Hometopic listById(Integer id) {

        Hometopic hometopic = this.getById(id);

        List<Topic_img> topicImgs = iTopicImgService.list();

        List<Top_img> topImgs = iTopImgService.list();

        ArrayList<Top_img> top_imgs = new ArrayList<>();

        ArrayList<Topic_img> topic_imgs = new ArrayList<>();
        for (Topic_img topic_img : topicImgs) {
            if(hometopic.getId()==topic_img.getParentId()){
                topic_imgs.add(topic_img);
            }
        }
        for (Top_img top_img : topImgs) {
            if(hometopic.getId()==top_img.getParentId()){
                top_imgs.add(top_img);
            }
        }
        hometopic.setTop_img(top_imgs);
        hometopic.setTopic_img(topic_imgs);

        return hometopic;
    }
}
